package tlv;

import java.util.ArrayList;
import java.util.List;

public class TlvTools {
	public static byte[] buildHwReqChallenge(int serialNo,String userIp,List<Attr> attr){
		TlvHwBean b = new TlvHwBean(2, 1, serialNo, 0, userIp, 0, attr.size(), attr, Constants.SECRET);
		return b.toByteArray();
	}
	public static byte[] buildHwReqAuth(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvHwBean b = new TlvHwBean(2, 3, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
		return b.toByteArray();
	}
	public static byte[] buildHwReqLogout(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvHwBean b = new TlvHwBean(2, 5, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
		return b.toByteArray();
	}
	public static byte[] buildHwAffAckauth(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvHwBean b = new TlvHwBean(2, 7, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
		return b.toByteArray();
	}
	public static byte[] buildHwReqInfo(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvHwBean b = new TlvHwBean(2, 9, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
		return b.toByteArray();
	}
	public static TlvHwBean parseHw(byte[] a){
		List<Attr> attr = new ArrayList<Attr>();
		int count = a[15];
		if(count>0){
			int t = 32;
			for (int i = 0; i < count; i++) {
				Attr c = new Attr();
				c.setAttrType(a[t]);
				c.setAttrLen(a[t+1]);
				byte[] d = new byte[c.getAttrLen()-2];
				System.arraycopy(a, t+2, d, 0, c.getAttrLen()-2);
				c.setAttrValue(new String(d));
				attr.add(c);
			}
		}
		TlvHwBean tlv = new TlvHwBean(a[0],a[1],a[2],a[3],(int)((a[5]<<8)|(a[4])), (int)((a[6]<<8)|(a[7])), (int)a[8]+"."+(int)a[9]+"."+(int)a[10]+"."+(int)a[11], (int)((a[13]<<8)|(a[12])),a[14], a[15], attr);
		return tlv;
	}

	public static byte[] buildReqChallenge(int serialNo,String userIp,List<Attr> attr){
		TlvBean b = new TlvBean(1, 1, serialNo, 0, userIp, 0, attr.size(), attr);
		return b.toByteArray();
	}
	public static byte[] buildReqAuth(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvBean b = new TlvBean(1, 3, serialNo, reqId, userIp, 0, attr.size(), attr);
		return b.toByteArray();
	}
	public static byte[] buildReqLogout(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvBean b = new TlvBean(1, 5, serialNo, reqId, userIp, 0, attr.size(), attr);
		return b.toByteArray();
	}
	public static byte[] buildAffAckauth(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvBean b = new TlvBean(1, 7, serialNo, reqId, userIp, 0, attr.size(), attr);
		return b.toByteArray();
	}
	public static byte[] buildReqInfo(int serialNo,int reqId,String userIp,List<Attr> attr){
		TlvBean b = new TlvBean(1, 9, serialNo, reqId, userIp, 0, attr.size(), attr);
		return b.toByteArray();
	}
	public static TlvBean parse(byte[] a){
		List<Attr> attr = new ArrayList<Attr>();
		int count = a[15];
		if(count>0){
			int t = 16;
			for (int i = 0; i < count; i++) {
				Attr c = new Attr();
				c.setAttrType(a[t]);
				c.setAttrLen(a[t+1]);
				byte[] d = new byte[c.getAttrLen()];
				System.arraycopy(a, t+2, d, 0, a.length);
				c.setAttrValue(new String(d));
				attr.add(c);
			}
		}
		TlvBean tlv = new TlvBean(a[0],a[1],a[2],a[3],(int)((a[5]<<8)|(a[4])), (int)((a[6]<<8)|(a[7])), (int)a[8]+"."+(int)a[9]+"."+(int)a[10]+"."+(int)a[11], (int)((a[13]<<8)|(a[12])),a[14], a[15], attr);
		return tlv;
	}
}
